﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLKS
{
    public partial class frxoasuaql : Form
    {
        private DataTable _dSQL;

        internal DataTable DSQL
        {
            get { return _dSQL; }
            set { _dSQL = value; }
        }

        private DataTable _loaiNV;

        internal DataTable LoaiNV
        {
            get { return _loaiNV; }
            set { _loaiNV = value; }
        }
        public frxoasuaql()
        {
            InitializeComponent();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frxoasuaql_Load(object sender, EventArgs e)
        {
            if(rdQl.Checked)
                DSQL = BUS.XuLyNhanVien.TimToanBoNhanVien("ql");
            if(rdNv.Checked)
                DSQL = BUS.XuLyNhanVien.TimToanBoNhanVien("nv");

            dgvDSQL.DataSource = DSQL;

            LoaiNV = BUS.XuLyChucVu.TimToanBoChucVu();
            List<String> MaLoaiNV = new List<string>();
            foreach (DataRow dr in LoaiNV.Rows)
            {
                MaLoaiNV.Add(dr["LoaiNhanVien"].ToString());
            }
            cbbMaLoaiNV.DataSource = MaLoaiNV;
        }

        private void dgvDSQL_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvDSQL.SelectedRows[0].Index;

            DataRow dr = DSQL.Rows[index];
            dtpNTNS.Value = DateTime.Parse(dr["NgaySinh"].ToString());
            txttenql.Text = dr["HoTen"].ToString();
            txtmaql.Text = dr["MaNhanVien"].ToString();
            txtdiachi.Text = dr["DiaChi"].ToString();
            txtMaLoai.Text = dr["MaLoaiNV"].ToString();
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            if (dgvDSQL.SelectedRows.Count > 0)
            {
                DTO.CNHANVIEN ql = new DTO.CNHANVIEN();
                ql.MaNhanVien = int.Parse(txtmaql.Text);
                ql.HoTen = txttenql.Text;
                ql.DiaChi = txtdiachi.Text;
                ql.NgaySinh = dtpNTNS.Value;
                TimeSpan diff = DateTime.Now - dtpNTNS.Value;
                ql.Tuoi = (int)(diff.Days / 365);
                ql.MaLoaiNhanVien = txtMaLoai.Text;

                if (!(BUS.XuLyNhanVien.SuaTTNhanVien(ql, ql.MaNhanVien)))
                    MessageBox.Show("Co loi xay ra. Kiem tra lai file CSDL");
                frxoasuaql_Load(sender, e);
            }
            else
                MessageBox.Show("Ban chua chon quan ly");
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbbMaLoaiNV_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int index = cbbMaLoaiNV.SelectedIndex;

                DataRow dr = LoaiNV.Rows[index];

                txtMaLoai.Text = dr["MaLoaiNV"].ToString();
            }
            catch (System.Exception )
            {
            	
            }
       }

        private void txtMaLoai_TextChanged(object sender, EventArgs e)
        {
            foreach (DataRow dr in LoaiNV.Rows)
            {
                if (dr["MaLoaiNV"].ToString() == txtMaLoai.Text)
                {
                    cbbMaLoaiNV.Text = dr["LoaiNhanVien"].ToString();
                }
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (dgvDSQL.SelectedRows.Count > 0)
            {
                int MaNV = int.Parse(txtmaql.Text);
                if (!(BUS.XuLyNhanVien.XoaNhanVien(MaNV)))
                    MessageBox.Show("Co loi xay ra. Kiem tra lai file CSDL");
                frxoasuaql_Load(sender, e);
            }
            else
                MessageBox.Show("Ban chua chon quan ly");
        }

        private void rdQl_CheckedChanged(object sender, EventArgs e)
        {
            frxoasuaql_Load(sender, e);
        }

        private void rdNv_CheckedChanged(object sender, EventArgs e)
        {
            frxoasuaql_Load(sender, e);
        }
    }
}
